//
//  AirButton.m
//  AirButtonView
//
//  Created by Air on 2018/8/3.
//  Copyright © 2018年 Air. All rights reserved.
//

#import "AirButton.h"

static CGFloat const kScale = 0.9;

@implementation AirButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageView.contentMode = UIViewContentModeCenter;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:12.0];
    }
    return self;
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    
    CGRect labelsize =[self.titleLabel.text boundingRectWithSize:CGSizeMake(self.bounds.size.width, self.bounds.size.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.titleLabel.font} context:nil];
    CGFloat width = labelsize.size.width;
    NSInteger length = self.titleLabel.text.length;
    width = (length <= 2 ? width : (width * kScale));
    return CGRectMake(width, 0.0, self.bounds.size.width, self.bounds.size.height);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    return CGRectMake(0.0, 0.0, self.bounds.size.width, self.frame.size.height);
}


@end
